package com.project.homework4_9.dao.impl;

import com.project.homework4_9.bean.LogForm;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

public class ProxyService implements InvocationHandler {
    private Object targetObj;
    public ProxyService(Object targetObj){
        this.targetObj=targetObj;
    }
    public Object getProxy(){
        return Proxy.newProxyInstance(
          targetObj.getClass().getClassLoader(),
          targetObj.getClass().getInterfaces(),this
        );
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            Object obj=method.invoke(targetObj,args);
            return obj;
        }catch (Exception e){
            //得到类名
        String content = targetObj.getClass().getName();
        content+=" 在执行"+method.getName()+"  时，抛出 \n";
        content+=e.getCause().toString()+"   异常";
        BaseDao dao=new BaseDao();
        dao.update(content);
    }
        return null;
    }
}
